package com.example.springboot_demo.service;

import com.alibaba.fastjson.JSON;
import com.example.springboot_demo.mappers.DevDocumentMapper;
import com.example.springboot_demo.pojo.DevDocument;
import com.example.springboot_demo.pojo.GovDocument;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

/**
 * 发改委
 */
@Component
public class DevService {

    @Autowired
    private DevDocumentMapper devDocumentMapper;


    List<String> userAgent = Arrays.asList("Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;AvantBrowser)",
            "Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;360SE)",
            "Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;Trident/4.0;SE2.XMetaSr1.0;SE2.XMetaSr1.0;.NETCLR2.0.50727;SE2.XMetaSr1.0)",
            "Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;TheWorld)",
            "Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;TencentTraveler4.0)",
            "Opera/9.80(Macintosh;IntelMacOSX10.6.8;U;en)Presto/2.8.131Version/11.11",
            "Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1",
            "Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
            "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36",
            "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1",
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
            "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
            "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
            "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
            "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
            "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
            "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
            "PostmanRuntime/7.28.4",
            "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52");


    public Set<String> getFromDb() {
        List<String> strings = devDocumentMapper.queryIds();
        return new HashSet<>(strings);
    }


    public List<Map<String, Object>> getPage(String keyWord, int year) {
        String timestamp = "1710816335870";
        String wordToken = "4d3185cdcbf68c0535aff362b7f6ae7d";
        //根据数字年份，得到当年1月1日和当年12月31日的文字。例如输入2017得到2017-01-01和2017-12-31
        String start = year + "-01-01";
        String end = year + "-12-31";
        Set<String> fromDb = getFromDb();
        int page = 1;
        String url = "https://sogw.ndrc.gov.cn/apiservice/s";
        while (true) {
            String request2 = "siteCode=bm04000007&tab=zwgk&timestamp=" +
                    timestamp +
                    "&wordToken=" +
                    wordToken +
                    "&page=" +
                    page +
                    "&pageSize=20&qt=" +
                    keyWord +
                    "&timeOption=0&sort=relevance&keyPlace=0&fileType=";
            //创建post请求对象
            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);

            String resultString = "";
            page++;

            try {
                // Set headers
                post.setHeader("suid", "348116bfda897f9dde88e43ca1aeaafa");
                post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

                // Set request body
                post.setEntity(new StringEntity(request2));

                //启动执行请求，并获得返回值
                CloseableHttpResponse response = client.execute(post);
                //得到返回的entity对象
                HttpEntity resBody = response.getEntity();
                //把实体对象转换为string
                resultString = EntityUtils.toString(resBody, "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            Map resMap = null;
            try {
                resMap = JSON.parseObject(resultString, Map.class);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(0);
                continue;
            }
            boolean flag = (boolean) resMap.get("ok");
            if (!flag) {
                System.out.println(resMap);
                System.exit(0);
            }
            Map<String, Object> data = (Map<String, Object>) resMap.get("data");
            Map<String, Object> search = (Map<String, Object>) data.get("search");
            if (search == null) {
                break;
            }
            List<Map<String, Object>> list = (List<Map<String, Object>>) search.get("searchs");
            if (CollectionUtils.isEmpty(list)) {
                break;
            }
            Integer total = (Integer) search.get("totalHits");
            int maxPage = (total + 19) / 20;

            System.out.println("当前年度：" + year + ", 关键词：" + keyWord + ", 总条数:" + total + ", 总页数：" + maxPage + ", 当前页数：" + page);
            for (Map<String, Object> item : list) {
                if (fromDb.contains((String) item.get("md5Url"))) {
                    continue;
                }
                DevDocument content = getContent(item);
                //插入数据库
                if (content != null) {
                    try {
                        devDocumentMapper.insert(content);
                        System.out.println("插入成功:" + content.getTitle());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (page > maxPage) {
                break;
            }
        }
        return null;
    }

    public DevDocument getContent(Map<String, Object> map) {
        HashMap<String, String> heards = new HashMap<>();
        //添加请求头参数
        heards.put("User-Agent", "userAgent.get(RandomUtils.nextInt(0, userAgent.size() - 1))");
        heards.put("Accept-Encoding", "gzip, deflate, br");
        heards.put("Connection", "keep-alive");
        heards.put("Accept", "application/json, text/plain, */*");
        heards.put("Accept-Encoding", "gzip, deflate, br");

        String url = (String) map.get("viewUrl");
        Document document = null;
        DevDocument govDocument = new DevDocument();
        govDocument.setUrl(url);
        govDocument.setId((String) map.get("md5Url"));
        govDocument.setTitle((String) map.get("title"));
        govDocument.setDocDate((String) map.get("docDate"));
        govDocument.setBelong((String) map.get("displayDb"));
        try {
            document = Jsoup.connect(url).headers(heards).get();
            // 来源
            Elements ent = document.select("div.ly.laiyuantext span");
            govDocument.setSource(ent.text());
            //文章内容处理
            Element content = document.select("div.TRS_Editor").first();
            if (content == null) {
                content = document.select("div.article_con").first();
            }
            if (content == null) {
                content = document.select("div.zwgkbg").first();
            }
            if (content == null) {
                System.out.println("没有内容：" + url);
            }
            if (content != null) {
                String text = content.text();
                StringBuilder stringBuilder = new StringBuilder();
                if (text.contains("兼并")) {
                    stringBuilder.append("兼并,");
                } else {
                    stringBuilder.append("--,");
                }
                if (text.contains("收购")) {
                    stringBuilder.append("收购,");
                } else {
                    stringBuilder.append("--,");
                }
                if (text.contains("并购")) {
                    stringBuilder.append("并购,");
                } else {
                    stringBuilder.append("--,");
                }
                if (text.contains("重组")) {
                    stringBuilder.append("重组");
                } else {
                    stringBuilder.append("--");
                }
                String tag = stringBuilder.toString();
                if (StringUtils.equals("--,--,--,--", tag)) {
                    return null;
                } else {
                    govDocument.setTag(tag);
                    govDocument.setContent("<!doctype html>\n" +
                            "<html>\n" +
                            content.outerHtml() +
                            "</html>"
                    );
                    return govDocument;
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost("https://sogw.ndrc.gov.cn/apiservice/s");

        // Set headers
        post.setHeader("suid", "348116bfda897f9dde88e43ca1aeaafa");
        post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        // Set request body
        String requestBody = "siteCode=bm04000007&tab=all&timestamp=1710686575412&wordToken=17fd995578fe3f51995bcc0e036417d0&page=1&pageSize=20&qt=%E5%B9%B6%E8%B4%AD&timeOption=2&sort=relevance&keyPlace=0&fileType=&startDateStr=2017-03-01&endDateStr=2024-03-18";
        post.setEntity(new StringEntity(requestBody));

        // Execute the request
        CloseableHttpResponse response = client.execute(post);

        // Get the response body
        HttpEntity resBody = response.getEntity();
        String resultString = EntityUtils.toString(resBody, "UTF-8");
        Map<String,Object> resMap = JSON.parseObject(resultString, Map.class);
        System.out.println(resMap);
    }
}
